package com.calsoft.Hops_Mobile_Tablet;
/*
 * User: Minh Diep
 * Date: 6/26/13
 * Time: 2:38 PM
 */

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import com.calsoft.Hops_Mobile_Tablet.base.App;
import com.calsoft.Hops_Mobile_Tablet.base.CardUI;
import com.calsoft.Hops_Mobile_Tablet.base.CardUIAdapter;
import com.calsoft.Hops_Mobile_Tablet.base.CardUIData;
import com.calsoft.Hops_Mobile_Tablet.sql.ProfileScheduleDbAdapter;
import com.calsoft.Hops_Mobile_Tablet.util.TimeUtil;

public class HomeDailyActivitiesFragment extends Fragment {

    private CardUIAdapter cardUIAdapter;
    private ProfileScheduleDbAdapter profileScheduleDbAdapter;
    private String dateStr;

    public HomeDailyActivitiesFragment(){

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //Set up the view
        View view = inflater.inflate(R.layout.fragment_home_daily, container, false);

        cardUIAdapter = new CardUIAdapter(getActivity().getApplicationContext());

        //Open our database
        dateStr = getArguments().getString(HomeSectionsPagerAdapter.ARG, "");
        if (dateStr.length() != 0){
            setUpData(dateStr);
        }
        //setUpDummyData();
        displayScheduleCardListView(view);

        return view;
    }

    private void setUpData(String dateStr) {
        profileScheduleDbAdapter = new ProfileScheduleDbAdapter(getActivity());
        profileScheduleDbAdapter.open();
        int numberOfRows = (int) profileScheduleDbAdapter.numberOfRows();
        Cursor cursor = profileScheduleDbAdapter.fetchAllScheduleByDate(dateStr);
        cursor.moveToFirst();
        int prevEndTime = App.DATE_START_TIME;
        String tempLastDesc = "";
        while (!cursor.isAfterLast()) {
            CardUIData cardUIData = new CardUIData(cursor);
            int duration = cardUIData.getStartTime() - prevEndTime;
            if (duration > App.SERVICE_MIN_TIME && !cardUIData.getCardDesc().equals("Arrival")){
                String timeDuration = TimeUtil.getTimeStringOfDay(prevEndTime) + " - " + cardUIData.getStartTimeStrFormat();
                CardUIData cardUIFreeTime = new CardUIData(++numberOfRows, CardUI.TYPE_ADD_SERVICE, "Free Time", prevEndTime, cardUIData.getStartTime(), timeDuration, "", "add_basic_blue");
                cardUIAdapter.addCardItem(cardUIFreeTime);
            }

            cardUIAdapter.addCardItem(cardUIData);
            tempLastDesc = cardUIData.getCardDesc();
            cursor.moveToNext();
            prevEndTime = cardUIData.getEndTime();
        }

        int duration = App.DATE_END_TIME - prevEndTime;
        if (duration > App.SERVICE_MIN_TIME && !tempLastDesc.equals("Departure")){
            String timeDuration = TimeUtil.getTimeStringOfDay(prevEndTime) + " - " + TimeUtil.getTimeStringOfDay(App.DATE_END_TIME);
            CardUIData cardUIFreeTime = new CardUIData(++numberOfRows, CardUI.TYPE_ADD_SERVICE, "Free Time", prevEndTime, App.DATE_END_TIME, timeDuration, "", "add_basic_blue");
            cardUIAdapter.addCardItem(cardUIFreeTime);
        }

        cursor.close();
    }

    @Override
    public void onStop() {
        super.onStop();
        profileScheduleDbAdapter.close();
    }

    private void displayScheduleCardListView(View view) {

        ListView listView = (ListView) view.findViewById(R.id.cards_list);

        listView.setAdapter(cardUIAdapter);
        listView.setDivider(null);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> listView, View view,
                                    int position, long id) {
                // Get the cursor, positioned to the corresponding row in the result set
                CardUIData itemClick = cardUIAdapter.getItem(position);
                if (itemClick.getType() != CardUI.TYPE_SERVICE_NON_CLICK && itemClick.getType() != CardUI.TYPE_HIS){
                    Intent intent = new Intent(getActivity(), CardUIDisplayActivity.class);
                    intent.putExtra(CardUI.CARD_DISPLAY_TYPE, itemClick.getType());
                    intent.putExtra(CardUI.CARD_ITEM_ID, itemClick.getItemId());
                    intent.putExtra(CardUI.CARD_ITEM_DATE, dateStr);
                    intent.putExtra(CardUI.CARD_ITEM_START_TIME, itemClick.getStartTime());
                    intent.putExtra(CardUI.CARD_ITEM_END_TIME, itemClick.getEndTime());
                    startActivity(intent);
                }
            }
        });
    }
}
